package com.wdd.atcrowdfunding.interceptor;

import com.wdd.atcrowdfunding.bean.Member;
import com.wdd.atcrowdfunding.bean.User;
import com.wdd.atcrowdfunding.util.Const;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.Set;

public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.定义那些路径是不需要拦截
        Set<String> uri = new HashSet<>();
        uri.add("/doLogin.do");  // 登录
        uri.add("/login.htm");
        uri.add("/logout.do"); //登出
        uri.add("/checkCode.do"); //验证码
        uri.add("/reg.htm");  //注册
        uri.add("/doReg.do");
        uri.add("/index.htm"); // 前台主页
            //请求路径
        String servletPath = request.getServletPath();
        if(uri.contains(servletPath)){
            return true;
        }


        //2.判断用户是否登录 会员是否登录
        HttpSession session = request.getSession();
        Object user = session.getAttribute(Const.LOGIN_USER);
        Object member = session.getAttribute(Const.LOGIN_MEMBER);
        if(user!=null || member!=null){
            return true;
        }else{
            response.sendRedirect(request.getContextPath()+"/login.htm");
            return false;
        }


    }
}
